package br.com.emmis.tercons.util;

import android.content.Context;
import android.os.AsyncTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

import br.com.emmis.tercons.constants.Constants;
import br.com.emmis.tercons.model.Config;

/**
 * Created by vaner on 04/02/2015.
 */
public class AmemUpdateUtil extends AsyncTask<String, Void, String> {


    private final Context mContext;

    public AmemUpdateUtil(Context ctx) {
        mContext = ctx;
    }

    @Override
    protected void onPostExecute(String result) {
        if(result != null && result.contains("OK")) {

        } else {

        }
    }

    @Override
    protected String doInBackground(String... ids) {

        String line = "FAIL";
        String id = ids[0];
        String url = Config.getInstance(mContext).getmIntentionURL();

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url + Constants.AMEM_INTENTIONS_URL);

            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair(Constants.XML_NODE_ID, id));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return line;
        }
    }
}